<?php

namespace app\admin\model\cabinet;

use think\Model;

class Lending extends Model
{
    // 表名
    protected $name = 'cabinet_lending';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
        'status_text',
        'runningdata_text',
        'loantime_text',
        'backtime_text',
        'support_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'),'1' => __('Status 1'),'-1' => __('Status -1')];
    }     

    public function getRunningdataList()
    {
        return ['-1' => __('Runningdata -1'),'0' => __('Runningdata 0'),'1' => __('Runningdata 1'),'2' => __('Runningdata 2'),'3' => __('Runningdata 3'),'4' => __('Runningdata 4'),'5' => __('Runningdata 5'),'6' => __('Runningdata 6')];
    }     

    public function getSupportList()
    {
        return ['1' => __('Support 1'),'2' => __('Support 2'),'3' => __('Support 3')];
    }     


    public function getStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRunningdataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['runningdata']) ? $data['runningdata'] : '');
        $valueArr = explode(',', $value);
        $list = $this->getRunningdataList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getLoantimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['loantime']) ? $data['loantime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getBacktimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['backtime']) ? $data['backtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSupportTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['support']) ? $data['support'] : '');
        $list = $this->getSupportList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setRunningdataAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    protected function setLoantimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setBacktimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function info()
    {
        return $this->belongsTo('Info', 'cabinet_info_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
